import {
	getBanner,
	getRcmd,
	getToplist,
	getPlaylistDetail
} from "@/api/api.js"
import {
	menuData
} from "./menu_data.js"
export default {
	data() {
		return {
			banner: [],
			meun_data: [],
			rcmd_data: [],
			top_data: [],
			top_detail: [],
			top_width: 0
		}
	},
	onLoad() {
		this.menu_data = menuData;
		this.init();

	},
	methods: {
		async init() {
			let banners = await getBanner();
			let rcmd = await getRcmd();
			let top = await getToplist();
			this.banner = banners.banners;
			this.rcmd_data = rcmd.result.splice(0, 6);
			this.top_data = top.list.splice(0, 4);

			for (let i = 0; i < this.top_data.length; i++) {
				let data = {
					id: this.top_data[i].id
				};
				let topDetail = await getPlaylistDetail(data);
				let myTracks = topDetail.playlist.tracks.splice(0, 3);
				this.$set(this.top_data[i], 'my_tracks', myTracks);

				for (let j = 0; j < myTracks.length; j++) {
					let artists = "";
					myTracks[j].ar.map(it => {
						artists += `${it.name}/`;
					});
					this.$set(this.top_data[i].my_tracks[j], 'my_ar', artists.slice(0, artists.length - 1));
				}
			}

			uni.createSelectorQuery().in(this).select('.toplist-box').boundingClientRect(data => {
				this.top_width = data.width;
			}).exec();
		},
		formatCount(count) {
			let cnt = Math.round(count / 10000);
			if (cnt >= 1) return cnt + "万";
			else return count;
		},
		goPlaylist(id) {
			const url = `/pages/playlist/playlist?id=${id}`;
			this.$navigateTo(url);
		},
		goMenu(item) {
			console.log(item);
			if (item.title == '排行榜') this.goRank();
			if (item.title == '猜歌名') this.$navigateTo('/pages/guessing-song-names/guessing-song-names')
			if (item.title == '小视频') this.$navigateTo('/pages/video/video')
		},
		goRank() {
			this.$navigateTo('/pages/rank/rank');
		},
		goSearch() {
			this.$navigateTo('/pages/search/search');
		}
	}
}